<?php
$section = 'parameter';
$titre = _LB_PARAMETER;
$smarty->assign("titre", $titre);

$tabs = array(	PARAMETER_TYPE_SYSTEM => _LB_SYSTEM
                );

$category = Request::getIntNumber('category')>count($tabs)||!Request::getIntNumber('category')?1:Request::getIntNumber('category');

$references = array();
if($category == PARAMETER_TYPE_TARGETING )
{
	// ADS-1169: Prepare data from Web Service
	// Format must obey by:
	// <?xml ...
	// <node>
	//   <cnode id="" value="" .. />
	//   <cnode id="" value="" .. />
	//   ...
	// </node>
	$p = new Parameter();
	$p->TYPE = PARAM_TYPE_LIST_WEBSERVICE;
	$p->find();
	while ($p->fetch()) {
		// Initialize the session
		$session = curl_init();

		global $g_user;
		$postData['uid'] = $g_user->ID;
		$postData['uip'] = urlencode($_SERVER['REMOTE_ADDR']);
		$postData['system'] = urlencode(APPLICATION_URL);

		// Set curl options
		curl_setopt($session, CURLOPT_URL, PROFILING_WEB_SERVICE. $p->EXTRA ."/id_partner/".ID_PARTENAIRE);
		curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($session, CURLOPT_HEADER, true);
		curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($session, CURLOPT_POST, 1);
		curl_setopt($session, CURLOPT_POSTFIELDS, $postData);

		// Make the request
		$response = curl_exec($session);

		// Close the curl session
		curl_close($session);

		// Get HTTP Status code from the response
		$status_code = array();
		preg_match('/\d\d\d/', $response, $status_code);

		// Get the XML from the response, bypassing the header
		if (!($xml = strstr($response, '<?xml'))) {
			$xml = null;
		}
		else
		{
			$x = simplexml_load_string($xml);

			$references[$p->ID] = array();
			foreach ($x->children() as $child) {
				$id = (int) $child['id'];
				$value = (string) $child['value'];
				$references[$p->ID][$id] = $value;
			}
		}
	}
}

// Prepare parameters
$param = new Parameter();
$param->CATEGORY = $category;
$param->orderBy('ID');
$param->find();

$cfer = new Cfer(array(
			_LB_ACCUEIL => Utils::getHomeLink(),
			_LB_PARAMETER.' : '.$tabs[$category] =>Utils::getCurrentLink()
		)
	);

$smarty->assign("references", $references);
$smarty->assign("cfer", $cfer);
$smarty->assign("param",$param);
$smarty->assign("category",$category);
$smarty->assign("tabs",$tabs);
$smarty->assign("numtabs",count($tabs));
$smarty->display('parameter_list.html');
